package com.qiyuan.web.controller.essay;

import java.util.List;
import java.util.Arrays;

import com.qiyuan.common.core.domain.entity.SysRole;
import com.qiyuan.essay.domain.BlogMusic;
import com.qiyuan.essay.domain.bo.BlogMusicBo;
import com.qiyuan.essay.domain.vo.BlogMusicVo;
import com.qiyuan.essay.service.IBlogMusicService;
import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.qiyuan.common.annotation.RepeatSubmit;
import com.qiyuan.common.annotation.Log;
import com.qiyuan.common.core.controller.BaseController;
import com.qiyuan.common.core.domain.PageQuery;
import com.qiyuan.common.core.domain.AjaxResult;
import com.qiyuan.common.core.validate.AddGroup;
import com.qiyuan.common.core.validate.EditGroup;
import com.qiyuan.common.core.validate.QueryGroup;
import com.qiyuan.common.enums.BusinessType;
import com.qiyuan.common.utils.poi.ExcelUtil;
import com.qiyuan.common.core.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiOperation;

/**
 * 音乐Controller
 *
 * @author ruoyi
 * @date 2022-04-22
 */
@Validated
@Api(value = "音乐控制器", tags = {"音乐管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/essay/music")
public class BlogMusicController extends BaseController {

    private final IBlogMusicService iBlogMusicService;

    /**
     * 查询音乐列表
     */
    @ApiOperation("查询音乐列表")
    @SaCheckPermission("essay:music:list")
    @GetMapping("/list")
    public TableDataInfo<BlogMusicVo> list(@Validated(QueryGroup.class) BlogMusicBo bo, PageQuery pageQuery) {
        return iBlogMusicService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出音乐列表
     */
    @ApiOperation("导出音乐列表")
    @SaCheckPermission("essay:music:export")
    @Log(title = "音乐", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(@Validated BlogMusicBo bo, HttpServletResponse response) {
        List<BlogMusicVo> list = iBlogMusicService.queryList(bo);
        ExcelUtil.exportExcel(list, "音乐", BlogMusicVo.class, response);
    }

    /**
     * 获取音乐详细信息
     */
    @ApiOperation("获取音乐详细信息")
    @SaCheckPermission("essay:music:query")
    @GetMapping("/{id}")
    public AjaxResult<BlogMusicVo> getInfo(@ApiParam("主键")
                                                  @NotNull(message = "主键不能为空")
                                                  @PathVariable("id") Integer id) {
        return AjaxResult.success(iBlogMusicService.queryById(id));
    }

    /**
     * 新增音乐
     */
    @ApiOperation("新增音乐")
    @SaCheckPermission("essay:music:add")
    @Log(title = "音乐", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public AjaxResult<Void> add(@Validated(AddGroup.class) @RequestBody BlogMusicBo bo) {
        return toAjax(iBlogMusicService.insertByBo(bo) ? 1 : 0);
    }

    /**
     * 修改音乐
     */
    @ApiOperation("修改音乐")
    @SaCheckPermission("essay:music:edit")
    @Log(title = "音乐", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public AjaxResult<Void> edit(@Validated(EditGroup.class) @RequestBody BlogMusicBo bo) {
        return toAjax(iBlogMusicService.updateByBo(bo) ? 1 : 0);
    }

    /**
     * 删除音乐
     */
    @ApiOperation("删除音乐")
    @SaCheckPermission("essay:music:remove")
    @Log(title = "音乐" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult<Void> remove(@ApiParam("主键串")
                                       @NotEmpty(message = "主键不能为空")
                                       @PathVariable Integer[] ids) {
        return toAjax(iBlogMusicService.deleteWithValidByIds(Arrays.asList(ids), true) ? 1 : 0);
    }

    /**
     * 状态修改
     */
    @ApiOperation("状态修改")
    @SaCheckPermission("essay:music:edit")
    @Log(title = "音乐", businessType = BusinessType.UPDATE)
    @PutMapping("/changeStatus")
    public AjaxResult<Void> changeStatus(@RequestBody BlogMusic music) {
        return toAjax(iBlogMusicService.updateMusicStatus(music));
    }
}
